

** make a table of bounds

/*


load and collapse to weekly sums
save as tempory files
stitch together 


*/

** pre-set the data - all  tests 
	use health/test_daily_panel, clear
	merge m:1 sid using health/demographics, keep(1 3) assert(2 3) ///
		nogen keepusing(sid age_wide)
	egen week = cut(date), at(`=mdy(3,13,2020)'(7)`=mdy(12,22,2020)')			
	drop if missing(week) 
	drop if missing(age_wide) | age_wide == 0
	merge m:1 age_wide using data/age_wide_counts, assert(3) nogen

	** tested/positive at least once in this week?
	collapse (max) positive tested, by(week age_wide sid n)	
	save health/test_week, replace 
		

** hospitalized 
	foreach group in no_icli any_major any_icli {
		
		* load the data		
		use health/inp_clean if ~no_dx, clear
		if "`group'" == "no_icli" gen no_icli = any_icli == 0
		if "`group'" == "pool_icli" gen pool_icli = 1
		keep if `group' == 1

		merge m:1 sid using health/demographics, assert(2 3) keep(1 3) ///
			keepusing(sid age_wide) nogen		
		drop if missing(age_wide) | age_wide == 0
		merge m:1 age_wide using data/age_wide_counts, assert(3) nogen
		rename n n_age_group
	
		egen week = cut(admit_date), at(`=mdy(3,13,2020)'(7)`=mdy(12,22,2020)')			
		drop if missing(week)
		keep test_tight pos_tight week age_wide n_age_group
		
		save health/inp_`group', replace  
		
			
		
	}
	timer clear 1
	timer on 1 
qui forvalues bs = 0 /10 {
	********************************************************************************
	********************************************************************************
	** load and save all tests
	********************************************************************************
	********************************************************************************
	** all tests
	use health/test_week 

	** resample 
	if `bs' > 0 bsample _N
	
	** aggregate, reweight 
	collapse (sum) tested positive, by(week age_wide n)	

	gen test_rate = tested / n
	gen conf_pos_rate = positive / n
	gen pos_rate = positive / tested

	
	** prepare group-specific variances for aggregation
	bysort week: egen tage = total(n)

	collapse (mean) test_rate conf_pos_rate pos_rate (rawsum) n [fw=n], by(week)
	

	format week %td		
	gen group ="All"
	save health/all_weekly_wtd_bs, replace 
		


	********************************************************************************
	********************************************************************************
	** load and save the hospital tests, by group
	********************************************************************************
	********************************************************************************
	foreach group in  no_icli any_major any_icli {
		
		* load the data
		use health/inp_`group', clear 							
		** resample 
		if `bs' > 0 bsample _N
			
		collapse (sum) test_tight pos_tight (count) n=test_tight, ///
			by(week age_wide n_age_group)
		gen test_rate = test_tight / n
		gen conf_pos_rate = pos_tight / n
		gen pos_rate = pos_tight / test_tight 

		collapse (mean) test_rate conf_pos_rate pos_rate ///
			(rawsum) n [fw=n_age_group], by(week)
		
		gen group = "`group'"
		save health/`group'_weekly_wtd_bs, replace

		format week %td
		format test_rate-pos_rate %4.3f
		
	}

	********************************************************************************
	********************************************************************************
	** append together etc
	********************************************************************************
	********************************************************************************

	clear
	foreach group in all any_icli no_icli any_major {
		append using health/`group'_weekly_wtd_bs
	}



	gen groupn = 1 if group == "All"
	replace groupn = 2 if group == "no_icli"
	replace groupn = 3 if group == "any_major"
	replace groupn = 4 if group == "any_icli"
	replace groupn = 5 if group == "pool_icli"

	rename conf_pos_rate lower_bound
	rename pos_rate upper_bound

	keep n test_rate group lower_bound upper_bound week groupn
	reshape wide n test_rate group lower_bound upper_bound, i(week) j(groupn)
	sort week  
	
	
	gen bs = `bs'
	# delimit ;
	keep bs week  
		lower_bound1 upper_bound1 
		lower_bound2 upper_bound2 
		lower_bound3 upper_bound3 
		lower_bound4 upper_bound4 
	;
	# delimit cr
	save bootstrap_ests/bs_test_`bs', replace 
}

timer off 1
timer list 1 

use bootstrap_ests/bs_test_0, clear 

format lower_bound*  %10.4f
format upper_bound* %10.3f
list week lower_bound1 upper_bound1 lower_bound2 upper_bound2 in 1/2


